package com.jrh.web.admin;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.jrh.biz.img.ImgUploadService;
import com.jrh.dal.AtlasDao;
import com.jrh.dal.AtlasDetailDao;
import com.jrh.dal.CategoryDao;
import com.jrh.dal.LabelDao;
import com.jrh.domain.entity.AtlasDetail;
import com.jrh.web.admin.form.AdminAtlasDetailForm;
import com.jrh.web.admin.form.AdminFormCopier;

/**
 * 美图集后台管理
 * 
 * @author jasen.hsu.cn@gmail.com 2012-7-23 下午3:55:16
 */
@Controller
@RequestMapping("/admin/atlas_detail")
public class AdminAtlasDetailController {

    private static final Integer pageSize             = 5;

    @Autowired
    private AtlasDao             atlasDao;

    @Autowired
    private AtlasDetailDao       atlasDetailDao;

    @Autowired
    private LabelDao             labelDao;

    @Autowired
    private CategoryDao          categoryDao;

    @Autowired
    private ImgUploadService     imgUploadService;

    private static final int     smallImgOutputWidth  = 50;

    private static final int     smallImgOutputHeight = 50;

    @RequestMapping(method = RequestMethod.GET, value = "/edit/{id}")
    public String edit(@PathVariable Long id, Model model) {
        AtlasDetail atlasDetail = atlasDetailDao.load(id);
        // 单个详情
        model.addAttribute("atlasDetail", atlasDetail);
        return "admin/atlas/detail/edit";
    }

    @RequestMapping(method = RequestMethod.POST, value = "/edit")
    public String doEdit(@ModelAttribute("atlasDetailForm") AdminAtlasDetailForm atlasDetailForm) {
        AtlasDetail atlasDetail = AdminFormCopier.convert(atlasDetailForm, null);
        if (atlasDetailForm != null && atlasDetailForm.getCoverImg() != null
            && atlasDetailForm.getCoverImg().getSize() > 0) {
            String coverImgPath = imgUploadService.upload(atlasDetailForm.getCoverImg(), "atlas_detail");
            String smallImgUrl = imgUploadService.resize(coverImgPath, smallImgOutputWidth, smallImgOutputHeight);
            atlasDetail.setSmallImgUrl(smallImgUrl);
            atlasDetail.setImgUrl(coverImgPath);
        }
        atlasDetailDao.update(atlasDetail);
        return "admin/success";
    }

    @RequestMapping(method = RequestMethod.POST, value = "/create")
    public String doCreate(@ModelAttribute("atlasDetailForm") AdminAtlasDetailForm atlasDetailForm) {
        if (atlasDetailForm == null || atlasDetailForm.getAtlasId() == null) {
            return "admin/error";
        }
        AtlasDetail atlasDetail = AdminFormCopier.convert(atlasDetailForm, null);
        if (atlasDetailForm != null && atlasDetailForm.getCoverImg() != null
            && atlasDetailForm.getCoverImg().getSize() > 0) {
            String coverImgPath = imgUploadService.upload(atlasDetailForm.getCoverImg(), "atlas_detail");
            String smallImgUrl = imgUploadService.resize(coverImgPath, smallImgOutputWidth, smallImgOutputHeight);
            atlasDetail.setSmallImgUrl(smallImgUrl);
            atlasDetail.setImgUrl(coverImgPath);
        }
        atlasDetailDao.create(atlasDetail);
        return "redirect:/admin/atlas/" + atlasDetailForm.getAtlasId() + ".htm";
    }

    /**
     * 删除
     * 
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable Long id) {
        AtlasDetail atlasDetail = atlasDetailDao.load(id);
        if (atlasDetail != null) {
            atlasDetailDao.delete(id);
            return "redirect:/admin/atlas/" + atlasDetail.getAtlasId() + ".htm";
        }
        return "admin/error";
    }

}
